package Test.View;

import Test.UserMapper;
import Test.User;
import Utils.MybatisUtils;
import org.apache.ibatis.session.SqlSession;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.*;
import java.io.IOException;
import java.util.Random;

import static Test.Util.findImageFiles;
import static Test.Util.getPublicIP;

public class Login extends JFrame implements MouseMotionListener,MouseListener{
    SqlSession sqlSession = MybatisUtils.getSqlSession();
    UserMapper usermapper = sqlSession.getMapper(UserMapper.class);
    public JLabel username = new JLabel("用户名：");
    public JTextField usernameInput = new JTextField("阿里木");
    public JPasswordField passwordInput = new JPasswordField("391543");
    public JButton resigned = new JButton();
    public JButton Login = new JButton();
    public Login() {
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        setBackground(Color.white);
        setTitle("登录");
        setBounds(100, 100, 751, 543);
        JPanel panel;
        JPanel contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(50, 50, 50, 50));
        contentPane.setLayout(new BorderLayout(0, 0));
        setContentPane(contentPane);
        String []imagei= findImageFiles("Images").toArray(new String[0]);
        Random r=new Random();
//        System.out.println(imagei[5]);
//        setBounds(100, 100, 753, 583);
        //imagei[r.nextInt(imagei.length)]
        ImageIcon background = new ImageIcon("src\\image\\backgroundimage.png");
        setSize(background.getIconWidth(),background.getIconHeight());
        panel = new JPanel(){
            @Override
            protected void paintComponent(Graphics g) {

                super.paintComponent(g);
                g.drawImage(background.getImage(), 0, 0, getWidth(), getHeight(), this);
                g.setFont(new Font("宋体", Font.BOLD,60));
                g.setColor(Color.RED);
            }
        };
        contentPane.setBorder(new EmptyBorder(0, 0, 0, 0));
        contentPane.setLayout(new BorderLayout(0, 0));
        setContentPane(contentPane);
        contentPane.setOpaque(false);
        panel.setBackground(Color.white);
        contentPane.add(panel, BorderLayout.CENTER);
        panel.setLayout(null);
        username.setFont(new Font("宋体", Font.PLAIN, 24));
        username.setBounds(background.getIconWidth()/3+25, background.getIconHeight()*2/7, 100, 38);
        username.setOpaque(false);
        panel.add(username);
        usernameInput.setFont(new Font("宋体", Font.PLAIN, 30));
        usernameInput.setSelectionColor(Color.RED);
        usernameInput.setBounds(background.getIconWidth()/2+25, background.getIconHeight()*2/7, 241, 38);
        usernameInput.setOpaque(false);
        usernameInput.setHorizontalAlignment(SwingConstants.CENTER);
        usernameInput.setForeground(Color.RED);
        panel.add(usernameInput);
        JLabel password = new JLabel("密码：");
        password.setFont(new Font("宋体", Font.PLAIN, 24));
        password.setBounds(background.getIconWidth()/3+50, background.getIconHeight()*3/7, 78, 38);
        password.setOpaque(false);
        panel.add(password);
        passwordInput.setFont(new Font("宋体", Font.PLAIN, 24));
        passwordInput.setBounds(background.getIconWidth()/2+25, background.getIconHeight()*3/7, 241, 38);
        passwordInput.setOpaque(false);
        passwordInput.setHorizontalAlignment(SwingConstants.CENTER);
        panel.add(passwordInput);
        resigned.setLabel("注册");
        resigned.setBounds(background.getIconWidth()*2/7,background.getIconHeight() * 5 / 8,192,31);
        panel.add(resigned);
        resigned.addMouseListener(this);
        this.addMouseMotionListener(this);
        resigned.setForeground(Color.RED);
        resigned.setContentAreaFilled(false);
        resigned.setFont(new Font("宋体", Font.PLAIN, 24));
        Login.setLabel("登录");
        Login.setBackground(Color.white);
        Login.setBounds(background.getIconWidth()*2/3, background.getIconHeight() * 5 / 8, 139, 31);
        panel.add(Login);
        Login.addMouseListener(this);
        this.addMouseMotionListener(this);
        this.setVisible(true);
        Login.setForeground(Color.RED);
        Login.setContentAreaFilled(false);
        Login.setFont(new Font("宋体", Font.PLAIN, 24));
        this.addComponentListener(new ComponentListener() {
            @Override
            public void componentResized(ComponentEvent e) {
                Dimension screenSize =getSize();
                username.setBounds(screenSize.width/3, screenSize.height*2/7, 100, 38);
                usernameInput.setBounds(screenSize.width/2, screenSize.height*2/7, 241, 38);
                passwordInput.setBounds(screenSize.width/2, screenSize.height*3/7, 241, 38);
                password.setBounds(screenSize.width/3, screenSize.height*3/7, 78, 38);
                resigned.setBounds(screenSize.width*2/7,screenSize.height * 5 / 8,192,31);
                Login.setBounds(screenSize.width*2/3, screenSize.height* 5 / 8, 139, 31);
            }
            @Override
            public void componentMoved(ComponentEvent e) {

            }

            @Override
            public void componentShown(ComponentEvent e) {

            }
            @Override
            public void componentHidden(ComponentEvent e) {
            }
        });

    }
    public void mouseClicked(MouseEvent e) {
        // TODO Auto-generated method stub
        if (e.getSource()== resigned){
            setVisible(false);
            new Resign().setVisible(true);
        }
        if (e.getSource()== Login){
            String text = usernameInput.getText();
            String text1 = passwordInput.getText();
//            System.out.println("用户名："+text+"\n密码："+text1);
            User user = usermapper.VerifyUser(new User(text, text1));
            user.setIP(getPublicIP());
            usermapper.UPDATEIP(user);
            setVisible(false);
            try {
                new ChatPage(text);
            } catch (IOException ex) {
                throw new RuntimeException(ex);
            }
        }
    }
    @Override
    public void mouseEntered(MouseEvent e) {
        // TODO Auto-generated method stub
    }
    @Override
    public void mouseExited(MouseEvent e) {
        // TODO Auto-generated method stub
    }
    @Override
    public void mousePressed(MouseEvent e) {
        // TODO Auto-generated method stub
    }
    @Override
    public void mouseReleased(MouseEvent e) {
        // TODO Auto-generated method stub
    }
    @Override
    public void mouseDragged(MouseEvent e) {
        // TODO Auto-generated method stub
    }
    @Override
    public void mouseMoved(MouseEvent e) {
        // TODO Auto-generated method stub
    }

    public static void main(String[] args) {
        new Login().setVisible(true);
    }

}

